package model.admin;
import com.opensymphony.xwork2.ActionSupport;
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.sql.*;

import javax.servlet.http.HttpSession;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import vo.*;
import vo.mixed.*;
import model.admin.*;
import myutil.*;
public class Common_Message {
int latest_document_id=0;
int latest_message_id=0;
Connect c;
ResultSet rs;
	public void update_message_table(ArrayList<String> receiver_ids,
			String body, String subject, File attached_file, String file_name) 
	{
		
		// TODO Auto-generated method stub
		try 
		{
			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			String uploader_email_id = session.getAttribute("username")
					.toString();
			if (attached_file != null) 
			{
				c = new Connect();
				// int uploader_id=0;
				
				c.dml("insert into document_upload(uploader_email_id,document_name) values('"
						+ uploader_email_id + "','" + file_name + "')");

				rs = c.getResult("select max(document_id) as max_id from document_upload");
				while (rs.next()) 
				{
					latest_document_id = rs.getInt("max_id");
					System.out.println(latest_document_id);
				}
				// System.out.println(getServlet().getServletContext().getRealPath(""));
				File f = new File(
						"C:/Users/SIVA KRISHNA/workspace/CampusMetalink/WebContent/attachments/"
								+ latest_document_id);
				f.mkdir();
				File f1 = new File(
						"C:/Users/SIVA KRISHNA/workspace/CampusMetalink/WebContent/attachments/"
								+ latest_document_id + "/" + file_name);
				FileUtils.copyFile(attached_file, f1);

				
			}
			c = new Connect();
			c.dml("insert into message(sender_id,body,subject,attachment_id) values('"+uploader_email_id+"','"+body+"','"+subject+"','"+latest_document_id+"')");
			rs=c.getResult("select max(message_id) as message_id from message");
			if(rs.next())
			{
				latest_message_id=rs.getInt("message_id");
			}
			for(String single_receiver:receiver_ids)
			{
				c.dml("insert into message_receiver(message_id,receiver_id) values('"+latest_message_id+"','"+single_receiver+"')");
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
